// README
//
// There are two steps to adding a property:
//
// 1. Create a member variable to store your property
// 2. Add the get_ and set_ accessors for your property
//
// Remember that both are case sensitive!


/// <reference name="MicrosoftAjaxTimer.debug.js" />
/// <reference name="MicrosoftAjaxWebForms.debug.js" />
/// <reference name="AjaxControlToolkit.ExtenderBase.BaseScripts.js" assembly="AjaxControlToolkit" />


Type.registerNamespace('DPRAjaxControlToolkit');

DPRAjaxControlToolkit.RSAEncryptBehavior = function(element) {
    DPRAjaxControlToolkit.RSAEncryptBehavior.initializeBase(this, [element]);

    // TODO : (Step 1) Add your property variables here
    this._SubmitHandler = null;
}
DPRAjaxControlToolkit.RSAEncryptBehavior.prototype = {
    initialize : function() {
        DPRAjaxControlToolkit.RSAEncryptBehavior.callBaseMethod(this, 'initialize');

        // TODO: Add your initalization code here
        var e = this.get_element();
        this._SubmitHandler = Function.createDelegate(this, this._OnSubmit);
        //x(x);
        //$addHandler(e.form, 'onsubmit', this._SubmitHandler);
        $addHandler(e.form, 'submit', this._SubmitHandler);
    },

    dispose : function() {
        // TODO: Add your cleanup code here
        var e = this.get_element();

        if (this._SubmitHandler != null){
            $removeHandler(e.form, 'submit', this._SubmitHandler);        
            this._SubmitHandler = null;
        }

        DPRAjaxControlToolkit.RSAEncryptBehavior.callBaseMethod(this, 'dispose');
    },
    
    get_Key : function() {
        setMaxDigits(131);
		return new RSAKeyPair(this._Exponent, "", this._Modulus);
	},
	
    _OnSubmit : function(evt) {
        //evt.preventDefault();

        var e = this.get_element();
        var key = this.get_Key();

        if (this.get_HasChallenge()) {
            this.set_EncryptedValue(encryptedString(key, this.get_ClientChallenge() + "\\" + base64encode(e.value)));
        } else {
            this.set_EncryptedValue(encryptedString(key, base64encode(e.value)));
        }        
				    
        this.set_ClientState(this.get_EncryptedValue()); 

	    e.value = "";

	    return;
    },

    // TODO: (Step 2) Add your property accessors here
    get_Exponent : function() {
        return this._Exponent;
    },

    set_Exponent : function(value) {
        this._Exponent = value;
    },

    get_Modulus : function() {
        return this._Modulus;
    },
    
    set_Modulus : function(value) {
        this._Modulus = value;
    },

    get_ClientChallenge : function() {
        return this._ClientChallenge;
    },
    
    set_ClientChallenge : function(value) {
        this._ClientChallenge = value;
    },

    get_HasChallenge : function() {
        return this._HasChallenge;
    },
    
    set_HasChallenge : function(value) {
        this._HasChallenge = value;
    },
    
    get_EncryptedValue : function() {
        return this._EncryptedValue;
    },

    set_EncryptedValue : function(value) {
        this._EncryptedValue = value;
    }
    
}
DPRAjaxControlToolkit.RSAEncryptBehavior.registerClass('DPRAjaxControlToolkit.RSAEncryptBehavior', AjaxControlToolkit.BehaviorBase);
